package com.ruoyi.basicdata.service.impl;

import java.util.List;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ruoyi.basicdata.domain.User;
import com.ruoyi.basicdata.mapper.UserMapper;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basicdata.mapper.ResidentMapper;
import com.ruoyi.basicdata.domain.Resident;
import com.ruoyi.basicdata.service.IResidentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 住户管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-06
 */
@Service
public class ResidentServiceImpl implements IResidentService 
{
    @Autowired
    private ResidentMapper residentMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询住户管理
     * 
     * @param id 住户管理主键
     * @return 住户管理
     */
    @Override
    public Resident selectResidentById(Long id)
    {
        return residentMapper.selectResidentById(id);
    }

    /**
     * 查询住户管理列表
     * 
     * @param resident 住户管理
     * @return 住户管理
     */
    @Override
    public List<Resident> selectResidentList(Resident resident)
    {
        return residentMapper.selectResidentList(resident);
    }

    /**
     * 新增住户管理
     * @param resident 住户管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertResident(Resident resident){
        // 添加住户之后，还要添加用户
        residentMapper.insertResident(resident);
        User user = new User();
        user.setReidentId(resident.getId());
        user.setUsername(resident.getResidentName());
        user.setPassword("123456");
        user.setPhone(resident.getResidentPhone());
        return userMapper.insertUser(user);
    }

    /**
     * 修改住户管理
     * 
     * @param resident 住户管理
     * @return 结果
     */
    @Override
    public int updateResident(Resident resident){
        return residentMapper.updateResident(resident);
    }

    /**
     * 批量删除住户管理
     * 
     * @param ids 需要删除的住户管理主键
     * @return 结果
     */
    @Override
    public int deleteResidentByIds(Long[] ids){
        userMapper.deleteUserByResidentIds(ids);
        return residentMapper.deleteResidentByIds(ids);
    }

    /**
     * 删除住户管理信息
     * 
     * @param id 住户管理主键
     * @return 结果
     */
    @Override
    public int deleteResidentById(Long id)
    {
        return residentMapper.deleteResidentById(id);
    }

    @Override
    public AjaxResult getCountByBuildId(Long id) {
        return residentMapper.getCountByBuildId(id);
    }

    @Override
    public List<Resident> getResidentByBuildingId(Long id) {
        return residentMapper.getResidentByBuildingId(id);
    }

    @Override
    public void updateBatchStateByBuildingId(Long id) {
        residentMapper.updateBatchStateByBuildingId(id);
    }

    @Override
    public AjaxResult changeState(Long id) {
       return residentMapper.changeState(id);
    }
}
